﻿#include "game.h"

game::game()
{
	credits = 0;
	bet = 1;
	playerWin = false;
	dealerWin = false;
}


game::~game()
{
}

void game::run(){
	system("cls");
	Console::gotoxy(5, 10);
	cout << "Welcome! Enter your name: ";
	cin >> name;
	Sleep(500);
	system("cls");
	Console::gotoxy(5, 10);
	cout << "Welcome, " << name << "!";
	Sleep(1500);
	Console::gotoxy(5, 11);
	cout << "The dealer hands you 100 credits";
	credits = 100;
	Sleep(1500);
	while (true){

		//setup
		playerWin = false;
		dealerWin = false;
		d.shuffle();
		player.clear();
		dealer.clear();

		//clear cin buffer
		cin.get();

		do{
			system("cls");
			Console::gotoxy(5, 11);
			if (credits == 0){
				cout << "You have no more credits!";
				Sleep(2000);
			}
			else{
				cout << "You have " << credits << " credits. ";
				Console::gotoxy(5, 12);
				cout << "Bet? (Max bet is 20, Enter 0 to quit): ";
				string line;
				getline(cin, line);

				// This code converts from string to number safely.
				stringstream myStream(line);
				if (myStream >> bet){
					if (bet == 0){
						break;
					}
					else if (bet > 20){
						Console::gotoxy(5, 13);
						cout << "Error - must bet between 0 - 20";
						Sleep(1000);
					}
					else if (bet > credits){
						Console::gotoxy(5, 13);
						cout << "Error - you dont have that many credits!";
						Sleep(1000);
						bet = 100;
					}
				}
				else{
					Console::gotoxy(5, 13);
					cout << "Invalid number, please try again" << endl;
					Sleep(1000);
					bet = 100;		// or any number above 20
				}
			}
		} while (bet > 20 && credits > 0);
		//quit if bet == 0
		if (bet == 0 || credits == 0){
			break;
		}

		credits -= bet;
		Console::gotoxy(5, 14);
		cout << "The dealer hands you 2 cards and draws 2 himself. ";
		int totalScore;
		player.push_back(d.draw());
		player.push_back(d.draw());
		dealer.push_back(d.draw());
		dealer.push_back(d.draw());
		flip = false;	// hide 2nd card
		Sleep(1000);
		
		//Player turn
		while (bet > 0 && playerWin == false && dealerWin == false){

			//Draw screen
			system("cls");
			print("Dealer");
			print("Player");

			//Check wins
			totalScore = 0;
			for (int i = 0; i < player.size(); i++){
				totalScore += player[i].getValue();
			}
			if (totalScore > 21){
				//check for aces to see if they lower the score
				for (int i = 0; i < player.size(); i++){
					if (totalScore > 21){
						if (player[i].getName() == "A"){
							totalScore -= 10;
						}
					}
				}
				//if totalscore still > 21
				if (totalScore > 21){
					Console::gotoxy(5, 16);
					cout << "Dealer Wins!";
					dealerWin = true;
					Sleep(800);
					break;
				}
			}
			if (totalScore == 21){
				Console::gotoxy(5, 16);
				cout << "You win!";
				playerWin = true;
				Sleep(800);
				break;
			}

			//Play
			Console::gotoxy(5, 14);
			cout << "Do you want to draw or stay? ";
			cin >> input;
			if (input == "draw" || input == "d"){
				player.push_back(d.draw());
				Console::gotoxy(5, 15);
				if (player.back().getColor() == '\x03' || player.back().getColor() == '\x04'){
					textClr('r');
					cout << "You draw " << player.back().getColor() << player.back().getName();
					textClr();
				}
				else{
					cout << "You draw " << player.back().getColor() << player.back().getName();
				}
				Sleep(1000);
			}
			else if (input == "stay" || input == "s"){
				break;
			}
			else{
				Console::gotoxy(5, 15);
				cout << "Error - must choose draw or stay!";
				Sleep(1000);
			}
		} // End player turn

		//Dealer turn
		//Must stay at 17 or higher
		while (playerWin == false && dealerWin == false){

			//Draw screen
			system("cls");
			print("Dealer");
			print("Player");

			//Check wins
			totalScore = 0;
			if (flip){
				for (int i = 0; i < dealer.size(); i++){
					totalScore += dealer[i].getValue();
				}
				if (totalScore > 21){
					//check for aces to see if they lower the score
					for (int i = 0; i < dealer.size(); i++){
						if (totalScore > 21){
							if (dealer[i].getName() == "A"){
								totalScore -= 10;
							}
						}
					}
					//if totalscore still > 21
					if (totalScore > 21){
						Console::gotoxy(5, 16);
						cout << "You Win!";
						playerWin = true;
						Sleep(800);
						break;
					}
				}
				if (totalScore == 21){
					Console::gotoxy(5, 16);
					cout << "Dealer wins!";
					dealerWin = true;
					Sleep(800);
					break;
				}
			}

			//Play
			if (totalScore < 17){
				if (flip){
					dealer.push_back(d.draw());
					Console::gotoxy(5, 14);
					if (dealer.back().getColor() == '\x03' || dealer.back().getColor() == '\x04'){
						textClr('r');
						cout << "Dealer draws " << dealer.back().getColor() << dealer.back().getName();
						textClr();
					}
					else{
						cout << "Dealer draws " << dealer.back().getColor() << dealer.back().getName();
					}
				}
				else{
					Console::gotoxy(5, 14);
					cout << "Dealer flips 2nd card.";
					flip = true;
				}
				Sleep(1000);
			}
			else{
				//Must stay at 17 or higher
				break;
			}
		}

		system("cls");
		print("Dealer");
		print("Player");

		//check win if both players < 21
		if (playerWin == false && dealerWin == false){
			int playertot = 0;
			int dealertot = 0;
			for (int i = 0; i < player.size(); i++){
				playertot += player[i].getValue();
			}
			for (int i = 0; i < dealer.size(); i++){
				dealertot += dealer[i].getValue();
			}
			Console::gotoxy(5, 16);
			if (playertot > dealertot){		
				cout << "You win!";
				playerWin = true;
			}
			else if (playertot == dealertot){
				cout << "Draw!";
				credits += bet;
			}
			else{
				cout << "Dealer Wins!";
			}
		}

		//pays credits to winner
		if (playerWin == true){
			credits += bet * 2;
		}

		Sleep(2000);
	}
}

void game::print(string cmd){
	int total = 0;
	if (cmd == "Dealer"){
		if (flip){
			for (int i = 0; i < dealer.size(); i++){
				Console::gotoxy(i * 3, 0);
				if (dealer[i].getColor() == '\x03' || dealer[i].getColor() == '\x04'){
					textClr('r');
					cout << dealer[i].getColor() << dealer[i].getName();
					textClr();
				}
				else{
					cout << dealer[i].getColor() << dealer[i].getName();
				}
				total += dealer[i].getValue();
				//check aces
				for (int i = 0; i < dealer.size(); i++){
					if (total > 21){
						if (dealer[i].getName() == "A"){
							total -= 10;
						}
					}
					else{
						break;
					}
				}				
			}
		}
		else{
			Console::gotoxy(0, 0);
			if (dealer[0].getColor() == '\x03' || dealer[0].getColor() == '\x04'){
				textClr('r');
				cout << dealer[0].getColor() << dealer[0].getName();
				textClr();
			}
			else{
				cout << dealer[0].getColor() << dealer[0].getName();
			}
			Console::gotoxy(3, 0);
			cout << "X";
			total = dealer[0].getValue();
		}
		Console::gotoxy(0, 1);
		cout << "Total = " << total;
	}
	else if (cmd == "Player"){
		for (int i = 0; i < player.size(); i++){
			Console::gotoxy(i*3, 2);
			if (player[i].getColor() == '\x03' || player[i].getColor() == '\x04'){
				textClr('r');
				cout << player[i].getColor() << player[i].getName();
				textClr();
			}
			else{
				cout << player[i].getColor() << player[i].getName();
			}
			total += player[i].getValue();
			for (int i = 0; i < player.size(); i++){
				if (total > 21){
					if (player[i].getName() == "A"){
						total -= 10;
					}
				}
				else{
					break;
				}
			}
		}
		Console::gotoxy(0, 3);
		cout << "Total = " << total;
	}
}

void game::textClr(char c){
	HANDLE hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	int k;
	switch (c){
	case 'r':
		k = 12;
		break;
	case 'b':
		k = 9;
		break;
	case 'g':
		k = 10;
		break;
	case 'y':
		k = 14;
		break;
	case 'w':
		k = 15;
		break;
	default:
		break;
	}
	SetConsoleTextAttribute(hConsole, k);
}